﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Northwind.NET.Utilities;
using Northwind.NET.EDM.FE.WinForms;
using Northwind.NET.EDM.FE.WinForms.Controls;

namespace Northwind.NET.EDM.FE
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

                SimpleLogWriter.ResetLog();

                if (!Northwind.NET.Settings.Globals.HideStartUpForm)
                {
                    StartUpForm startUpForm = new StartUpForm();
                    startUpForm.ShowDialog();
                }

                //Application.Run(new MainSwitchboardForm());
                Application.Run(new UserControlManagerForm2());

            }
            catch (ApplicationException ex)
            {
                string caption = string.Format(Northwind.NET.Settings.Resources.StartingApplicationMessage, Northwind.NET.Settings.Globals.APPLICATION_TITLE);
                MessageBox.Show(ex.Message, caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                SimpleLogWriter.WriteLogLine(ex.Message);
            }
            catch (Exception ex)
            {
                string caption = string.Format(Northwind.NET.Settings.Resources.StartingApplicationMessage, Northwind.NET.Settings.Globals.APPLICATION_TITLE);
                MessageBox.Show(ex.Message + System.Environment.NewLine + System.Environment.NewLine +
                                ex.StackTrace, caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                SimpleLogWriter.WriteLogLine(Northwind.NET.Settings.Resources.RuntimeErrorLogRecordsSeparatorString);
                SimpleLogWriter.WriteLogLine(ex.StackTrace);
            }
        }

        /// <summary>
        /// Global (catch-all) error handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            string caption = Northwind.NET.Settings.Globals.APPLICATION_TITLE;

            if (e.Exception is ApplicationException)
            {
                MessageBox.Show(e.Exception.Message, caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                SimpleLogWriter.WriteLogLine(e.Exception.Message);
                return; 
            }

            Exception ex = e.Exception;

            string message = "";
            string stackTrace = "";

            if (ex.InnerException != null)
            {
                message = ex.InnerException.Message + " - ";
                stackTrace = ex.InnerException.StackTrace;   
            }
            else
            {
                message += ex.Message;
                stackTrace = ex.StackTrace; 
            }

            MessageBox.Show(message + System.Environment.NewLine +  System.Environment.NewLine +
                            ex.StackTrace, caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);

            SimpleLogWriter.WriteLogLine(Northwind.NET.Settings.Resources.RuntimeErrorLogRecordsSeparatorString);
            SimpleLogWriter.WriteLogLine(ex.StackTrace);   
        }
    }
}
